// pages/mode/mode.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tableList: [],
    result: [],
    num: 0,
    time: 2 * 60 * 1000,
    timer: 2,
    textTime: '',
    bfb: 0,
    show: false,
  },
  //提交
  submit() {
    let tableList = this.data.tableList;
    // let tbnum = tableList.reduce((val, item, idx) => {
    //   let numtotal = 0;
    //   if (item.answer) {
    //     if (item.answer == item.answers) {
    //       // console.log("Y")
    //       return numtotal += 5
    //     }
    //   }
    // }, 0)
    let numtotal = 0;
    tableList.forEach((item) => {
      if (item.answer) {
        if (item.answer == item.answers) {
          // console.log("Y")
          return numtotal += 5
        }
      }
    })
    console.log(numtotal)
  },
  //点击序列号跳转
  serial(event) {
    // console.log(event.target.dataset.serial)
    this.setData({
      num: event.target.dataset.serial,
      show: false
    })
  },
  //显示
  showPopup() {
    this.setData({
      show: true
    });
  },
  //隐藏
  onClose() {
    this.setData({
      show: false
    });
  },
  //计算时间
  timers() {
    let oldTime = this.data.timer * 60
    let newtimers = oldTime;
    let newtimer = setInterval(() => {
      let hours = Math.floor(newtimers / 3600);
      let min = Math.floor(newtimers / 60);
      let s = newtimers % 60;
      if (newtimers > 0) {
        newtimers--;
      } else {
        clearInterval(newtimer)
      }

      this.setData({
        timer: newtimers,
        textTime: this.fill0(hours) + ":" + this.fill0(min) + ":" + this.fill0(s),
        bfb: Math.floor(newtimers / oldTime * 100)
      })
      // console.log(hours + ":" + min + ":" + s)
    }, 1000)
  },
  // 补零
  fill0(val) {
    return val < 10 ? '0' + val : val
  },
  //复选框
  onChange(event) {
    // console.log(event.detail)
    let num = this.data.num;
    this.setData({
      result: event.detail,
    });
    this.setData({
      ['tableList[' + num + '].answer']: JSON.stringify(this.data.result)
    });
    // console.log(this.data.tableList[num].answer)

  },
  //下一题
  nextt() {
    if (this.data.num < this.data.tableList.length - 1) {
      // console.log(this.data.result)
      this.setData({
        result: [],
        num: this.data.num += 1,
      })
      if (this.data.tableList[this.data.num].answer) {
        this.setData({
          result: JSON.parse(this.data.tableList[this.data.num].answer)
        })
      }
    }
    console.log(this.data.tableList)
  },
  //上一题
  restore() {
    let num = this.data.num;
    if (num > 0) {
      this.setData({
        num: num -= 1,
      })
      if (this.data.tableList[num].answer) {
        this.setData({
          result: JSON.parse(this.data.tableList[num].answer)
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.timers()
    wx.request({
      url: 'http://localhost:3001/questions/randList',
      data: {},
      header: {},
      success: (res) => {
        let resData = res.data.data;
        resData.forEach(item => {
          item.options = JSON.parse(item.options)
          // item.answer = []
          // console.log(item)
        })
        this.setData({
          tableList: resData
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})